<template>
  <div class="search">
    <el-form
      label-width="80px"
      label-position="left"
      class="form"
      size="large"
      :model="searchFrom"
      ref="elFormRef"
    >
      <!-- 表单-->
      <el-row :gutter="20">
        <template v-for="item in searchConfig.formItem" :key="item.prop">
          <el-col :span="8">
            <el-form-item :prop="item.prop" :label="item.label" clearable>
              <template v-if="item.type === 'input'">
                <el-input v-model="searchFrom[item.prop]" :placeholder="item.placeholde" />
              </template>

              <template v-if="item.type === 'date-picker'">
                <el-date-picker
                  type="daterange"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  v-model="searchFrom[item.prop]"
                />
              </template>

              <template v-if="item.type === 'select'">
                <el-select
                  placeholder="请选择状态"
                  size="large"
                  style="width: 100%"
                  v-model="searchFrom[item.prop]"
                >
                  <template v-for="option in item.options" :key="option.value">
                    <el-option :label="option.label" :value="option.value" />
                  </template>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>

      <!-- 按钮 -->
      <div class="btns">
        <el-button type="info" size="large" icon="Delete" @click="resetClick">重置</el-button>
        <el-button type="primary" size="large" icon="Search" @click="searchClick">搜索</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, defineEmits, defineProps } from 'vue'
import type { ElForm } from 'element-plus'

interface IProps {
  searchConfig: {
    formItem: any[]
  }
}

const emits = defineEmits(['queryClick', 'resetClick'])
const props = defineProps<IProps>()
const searchFrom = reactive({})

const elFormRef = ref<InstanceType<typeof ElForm>>()

function searchClick() {
  emits('queryClick', searchFrom)
}
function resetClick() {
  elFormRef.value.resetFields()
  emits('resetClick')
}
</script>

<style lang="scss" scoped>
.search {
  background-color: #fff;
  padding: 20px;
  .el-form-item {
    padding: 20px 30px;
    margin-bottom: 0;
  }
  .btns {
    text-align: right;
    padding: 0 50px 10px 0;
  }
}
</style>
